package com.eascs.archive_query.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.credit.entity.CreditReport;
import com.eascs.admin.o2o.credit.service.CreditReportService;
import com.eascs.admin.o2o.detail.form.DetailFrom;
import com.eascs.admin.o2o.detail.form.ShowDetailForm;
import com.eascs.admin.o2o.detail.service.LoanDetailService;
import com.eascs.admin.o2o.enterpriseZX.entity.O2oEnterpriseZxEntity;
import com.eascs.admin.o2o.enterpriseZX.service.O2oEnterpriseZxService;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.internal.service.PubDditemService;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanUserEntity;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanUserService;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.specialInterview.service.O2OSpecialInterviewService;
import com.eascs.afterloan.service.AdvanceSettleService;
import com.eascs.afterloancheck.service.LoanCheckServicce;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.brand.service.O2oBrandService;
import com.eascs.brand.vo.BrandAndPartnerVo;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.crm.nsEditDetail.service.CustomerAssetInfoService;
import com.eascs.crm.nsEditDetail.service.CustomerShopInfoService;
import com.eascs.gaczcontract.entity.CustomerBasicInfo;
import com.eascs.loan.entity.O2oBankLoanData;
import com.eascs.loan.service.O2oLoanService;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.type.SystemModule;

/**
 * @author 刘强
 * @describe 归档查询二期
 * @param showDetailForm
 * @return
 */
@Controller
public class ArchiveDetailController extends BaseMasterViewController{
	private static Logger logger = LoggerFactory.getLogger(ArchiveDetailController.class);
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Autowired
	private O2oLoanService o2oLoanService;
	@Autowired
	private O2oLoanUserService o2oLoanUserService;
	@Reference(retries=3)
	ICustomerInfoService iCustomerInfoService;
	@Autowired
	private O2oEnterpriseZxService o2oEnterpriseZxService;
	@Autowired
	private BaseUtils baseUtils;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;
	@Value("${o2o.admin.iframe.domain}")
	private String domain;
	@Value("${sd.url}")
	private String sdUrl;
	@Value("${oa.search.url}")
	private String oaUrl;
	@Autowired
	private CreditReportService creditReportService;
	@Autowired
	private LoanDetailService loanDetailService;
	@Value("${oa.zx.url}")
	private String oaZxUrl;
	@Value("${oa.ns.url}")
	private String oaNsUrl;
	@Value("${oa.yh.url}")
	private String oaYhUrl;
	@Value("${oa.fk.url}")
	private String oaFkUrl;
	@Value("${oa.bzj.url}")
	private String oaBzjUrl;
	@Autowired
	private O2oBrandService o2oBrandService;
	@Autowired
	LoanCheckServicce loanCheckServicce;
	@Autowired
	O2OSpecialInterviewService o2OSpecialInterviewService;
	@Autowired
	AdvanceSettleService advanceSettleService;
	@Autowired
	CustomerShopInfoService shopInfoService;
	@Autowired
	CustomerAssetInfoService assetInfoService;
	@Autowired
	PubDditemService pubDditemService;
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	
	@RequestMapping(value = "/archiveDetail", method = RequestMethod.GET)
	public String showDetail(ShowDetailForm showDetailForm) {
		O2oLoanapplyEntity loanapply = null;

		if (!StringUtil.isEmpty(showDetailForm.getBillCode())) {
			loanapply = o2oloanapplyService.fingByBillCodeDetail(showDetailForm.getBillCode());
			if (loanapply == null) {
				// 没有权限返回错误页面
				return "notauthView";
			}
			showDetailForm.setLoanapplyNo(loanapply.getLoanapplyNo());
		}
		loanapply = o2oloanapplyService.findByIdDetail(showDetailForm.getLoanapplyNo());
		// loanapply.setSettleDate(com.eascs.admin.common.utils.StringUtils.parseDate(loanapply.getSettleDate()));
		if (loanapply != null) {// 控制数据权限，空则没有权限，不空则有权限
			CreditReport creditReport = creditReportService.queryLoanapplyNo(showDetailForm.getLoanapplyNo(),
					String.valueOf(StatusContexts.O2O_ZERO));
			O2oLoanUserEntity materEntity = o2oLoanUserService.findSpouseId(loanapply.getLoanId(),
					showDetailForm.getLoanapplyNo());
			// 查询是否已交保证金、管理费、风险保证金
			ViewContext.put("loanStatus", loanapplyExpandService.queryLoanapplyNo(showDetailForm.getLoanapplyNo()));
			// 审批详情
			String agreeUrl = baseUtils.getApprovalAgreeUrl(loanapply.getLoanapplyNo(), null,
					SystemModule.SYSTEM_MODULE_O2O);
			O2oLoanUserEntity loanUser = o2oLoanUserService.findById(loanapply.getLoanId(), loanapply.getLoanapplyNo());
			O2oLoanUserEntity loanSorpouseUser = o2oLoanUserService.findSpouseId(loanapply.getLoanId(),
					loanapply.getLoanapplyNo());
			O2oBankLoanData bankData = o2oLoanService.queryBillCode(loanapply.getBillCode());
			BrandAndPartnerVo brand = o2oBrandService.queryCodeAndName(loanapply.getLoanapplyNo());
			O2oEnterpriseZxEntity O2oEnterpriseZxEntity= o2oEnterpriseZxService.getEntityByloanapply(loanapply.getLoanapplyNo());
			ViewContext.put("O2oEnterpriseZxEntity", O2oEnterpriseZxEntity);
			ViewContext.put("bankData", bankData);
			ViewContext.put("loanUser", loanUser);
			ViewContext.put("loanSorpouseUser", loanSorpouseUser);
			ViewContext.put("agreeUrl", agreeUrl);
			ViewContext.put("domain", domain);
			ViewContext.put("materEntity", materEntity);
			ViewContext.put("brand", brand);
			String isNotContinue = null;
			O2oLoanapplyEntity loanapplyEntity = loanDetailService.queryLoanapplyBillCode(loanapply.getLoanapplyNo(),
					loanapply.getLoanId(), loanapply.getBillCode(), loanapply.getCreateDate());
			if (loanapplyEntity != null) {
				loanapply.setContinueLoanParenNo(loanapplyEntity.getBillCode());
				isNotContinue = "Y";
			}
			// 面签申报通过才能发起贷后检查
			String loanFlg = loanapply.getLoanFlg();

			if (loanFlg != null && "Y".equals(loanFlg) && !"HN".equals(loanapply.getWfType())) {
				Boolean flag = loanCheckServicce.isStartFlow(loanapply.getBillCode());
				if (!flag) {
					ViewContext.put("ableCheck", "true");
				}
			}
			ViewContext.put("isNotContinue", isNotContinue);
			ViewContext.put("loanapply", loanapply);
			ViewContext.put("creditReport", creditReport);
			User user = UserHolder.getUser();
			ViewContext.put("userNo", user.getNo());

			// =================借款人/配偶征信报告 begin=================
			String userZxId = "";
			String mateZxId = "";
			if (loanUser != null) {
				userZxId = loanUser.getZxNo();
			}
			if (materEntity != null) {
				mateZxId = materEntity.getZxNo();
			}

			ViewContext.put("userZxId", userZxId);
			ViewContext.put("mateZxId", mateZxId);
			//新增parameter
			ViewContext.put("parameter", showDetailForm.getLoanapplyNo());//实验贷款要素能否取到loanapplyNo(主键)
			// =================借款人/配偶征信报告 end=================

			return "archive_query/archive_queryDetail";

		} else {// 没有权限返回错误页面
			return "notauthView";
		}
	}
	/**
	 * 借款人信息_归档查询详情
	 * 
	 * @param detailForm
	 * @return
	 */
	@RequestMapping(value = "/archiveDetail/queryArchiveLoanUser", method = RequestMethod.GET)
	public String queryArchiveLoanUser(DetailFrom detailForm, String type, String approvalStatus) {
		O2oLoanapplyEntity loanapply = o2oloanapplyService.findById(detailForm.getLoanapplyNo());
		O2oLoanUserEntity loanUser = o2oloanapplyService.findLoanUserEntity(detailForm.getLoanapplyNo(), detailForm.getLoanId());
		CustomerBasicInfo customerBasicInfo = o2oloanapplyService.getCustomerEntityById(loanUser.getId());
		String lastPaymentTime = o2oloanapplyService.getLastPaymentTime(detailForm.getLoanapplyNo(), detailForm.getLoanId());// 查询上一次放款时间
		if(!StringUtil.isEmpty(lastPaymentTime)){
			ViewContext.put("lastPaymentTime", lastPaymentTime);
		}else {
			ViewContext.put("lastPaymentTime", "无");
		}
		int loanCount = o2oLoanUserService.getLoanerCount(detailForm.getLoanId());
		String firstLoanTime = o2oLoanUserService.getStartLoanTime(detailForm.getLoanId());
		if ( "M".equals(getSex(loanUser.getDocumentNo())) ) {
			ViewContext.put("show_sex", "男");
		} else if ( "F".equals(getSex(loanUser.getDocumentNo())) ) {
			ViewContext.put("show_sex", "女");
		}
		ViewContext.put("loanUser", loanUser);
		ViewContext.put("type", type);
		ViewContext.put("approvalStatus", approvalStatus);
		ViewContext.put("basic", customerBasicInfo);
		ViewContext.put("loanapply", loanapply);
		ViewContext.put("numberOfLoans", loanCount);
		ViewContext.put("firstLoanTime", firstLoanTime);
		
		return "archive_query/loanUser_finance";
	}
	// 根据身份证信息获取年龄
	public String getSex(String UUserCard) {
		// 获取出生日期
		// UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
		// 获取性别
		if (Integer.parseInt(UUserCard.substring(16, 17)) % 2 == 1) {
			// alert("男");
			return "M";
		} else {
			// alert("女");
			return "F";
		}
	}
}
